package com.example.config;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

/**
 * 注册枚举类型转换工厂
 * @ClassName: MyConvertFactoryConfigurer
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author liuwj3
 * @date 2019年2月15日 上午10:48:01
 *
 */
@Configuration
public class MyConvertFactoryConfigurer {
	@Autowired
	private RequestMappingHandlerAdapter handlerAdapter;
	
	@Autowired
	private StringToEnumFactory stringToEnumFactory;
	
	/**
	 * 注册到GenericConversionService中
	 */
	@PostConstruct
	public void addConversionConfig() {
		ConfigurableWebBindingInitializer  initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
		if (initializer != null && initializer.getConversionService() != null) {
			GenericConversionService conversionService = (GenericConversionService) initializer.getConversionService();
			if (conversionService != null) {
				conversionService.addConverterFactory(stringToEnumFactory);
			}
		}
		
		// springmvc中注册方式
//		<!-- 通过:annotation-driven的conversion-service属性来装配我们的类型转换器 -->
//		<mvc:annotation-driven conversion-service="factoryBean" />
//		<!-- 通过ConversionServiceFactoryBean注册我们的自定义转换器 -->
//		<bean id="factoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean" >
//		    <property name="converters"><!-- 在属性converters注册 -->
//		        <list>
//		            <!--这里只要注册我们自定义的转换器工厂即可-->
//		            <bean class="com.mvc.convertor.MyConvertorFactory" />
//		        </list>
//		    </property>
//		</bean> 
	}
	
}
